package com.lm.redisson.controller;

import jakarta.annotation.Resource;
import org.redisson.api.RBlockingDeque;
import org.redisson.api.RDelayedQueue;
import org.redisson.api.RedissonClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/queue")
public class QueueController {

    @Resource
    private RedissonClient redissonClient;

    @GetMapping("/add")
    public String addDelayJob() {
        RBlockingDeque<String> blockingDeque = redissonClient.getBlockingDeque("delay-queue");
        RDelayedQueue<String> delayedQueue = redissonClient.getDelayedQueue(blockingDeque);

        delayedQueue.offer("任务-" + System.currentTimeMillis(), 10, TimeUnit.SECONDS);
        return "任务已加入延迟队列（10 秒后执行）";
    }

    @GetMapping("/take")
    public String takeJob() throws InterruptedException {
        RBlockingDeque<String> blockingDeque = redissonClient.getBlockingDeque("delay-queue");
        return "取出任务：" + blockingDeque.take();
    }
}

